$(function(){
	var jude = false;
	$("#logo").click(function(){
		jude = false;
		$("#side-menu .nav-header").addClass("nav-open");
		$("#side-menu .nav-open").click();
	})
	$("#side-menu").on("click",".nav-open",function(){
		if(jude){
//			$("#side-menu .nav-header").css("background","#f38a46");
			$("#side-menu .nav-header").removeClass("nav-open");
			$("#navbar-open").click();
		}else{
			jude = true;
		}
	});
})
//获取菜单层级
var menuLevel =function(level){
	if(level == 2){
		return "second";
	}
	if(level == 3){
		return "third";
	}
}

//判断子节点中是否含有菜单节点
var existsMenu = function(clildMenus){
	if(null != clildMenus && clildMenus.length > 0){
		for(var i = 0; i < clildMenus.length ; i++){
			if(clildMenus[i].menuType == "0"){
				return true;
			}
		}
	}
	return false;
}

//递归子菜单
var recursionMenus = function(level,userMenus){
	//子菜单定位到3级菜单
	if(level > 3){
		return "";
	}
	var menuDoc = '<ul class="nav nav-'+menuLevel(level)+'-level">';
	for(var i = 0; i < userMenus.length ; i++){
		var menu = userMenus[i];
		//过滤按钮节点
		if(menu.menuType != "0"){
			continue;
		}
		var clildMenus = menu.childMenus;
		menuDoc += '<li>';
		menuDoc += '<a '+((menu.menuPath != "#")?'class="J_menuItem"':'')+' href="'+menu.menuPath+'">' + menu.menuName;
		if(existsMenu(clildMenus)){
			menuDoc += '<span class="fa arrow"></span>';
			menuDoc += '</a>';
			menuDoc += recursionMenus(level+1,menu.childMenus);
		}else{
			menuDoc += '</a>';
		}
		menuDoc += '</li>';
	}
	menuDoc += '</ul>';
	return menuDoc;
}



/**
 * 初始化菜单项
 */
var loadMenu = function(data){
	$("#userName").html(data.name);
	var userMenus = data.userMenus;
	//用户菜单
	if(null != userMenus && userMenus.length > 0){
		var menuDoc = "";
		for(var i = 0; i < userMenus.length ; i++){
			var menu = userMenus[i];
			//过滤按钮节点
			if(menu.menuType != "0"){
				continue;
			}
			var clildMenus = menu.childMenus;
			menuDoc += "<li>";
			menuDoc += '<a '+ ((menu.menuPath != "#")?'class="J_menuItem"':'') +' href="'+menu.menuPath+'">';
			menuDoc += '<i class="'+menu.extendField1+'"></i>';
			menuDoc += '<span class="nav-label">'+menu.menuName+'</span>';
			if(existsMenu(clildMenus)){
				menuDoc += '<span class="fa arrow"></span>';
				menuDoc += "</a>";
				menuDoc += recursionMenus(2,clildMenus);
			}else{
				menuDoc += "</a>";
			}
			menuDoc += "</li>";
		}
		$("#side-menu").append(menuDoc);
		//左边菜单点击判断缓存用户是否存在
		$("#side-menu").find(".J_menuItem").click(function(){
			//获取缓存用户
			var userInfo = CommonUtils.getTempData("userInfo","");
			if(null == userInfo){
				//跳转页面
				window.location.href="../login.html";
			}
		});
	}
}

var userInfo = CommonUtils.getTempData("userInfo");
if(userInfo && null != userInfo){
	loadMenu(JSON.parse(userInfo));
}else{
	//获取用户信息
	$.ajax({url:'/init/user/findLoginUser',
		data:CommonUtils.encapsulationData({}),
		type:'post',  
		dataType:'json',  
		async:false,//同步请求
		contentType: "application/json",
		success:function(data) {
			if (data && null != data) {
				loadMenu(data);
			}
		},
		error : function(req, status, err) {  
			//消息提示款
			errorFunc(req, status, err);
		}  
	});
}


//退出系统
var quit = function(){
	//删除数据模式窗口
	swal({
		title : "您，确定要退出系统吗",
		text : "",
		type : "warning",
		showCancelButton : true,
		confirmButtonColor : "#DD6B55",
        cancelButtonText : "取消",
		confirmButtonText : "退出",
		closeOnConfirm : false
	}, function() {
		//退出系统
		$.ajax({url:'/init/user/quit',
			data:CommonUtils.encapsulationData({}),
			type:'post',  
			dataType:'json',  
			async:false,//同步请求
			contentType: "application/json",
			success:function(data) {
				//清理缓存
				CommonUtils.setTempData("userInfo","");
				//跳转页面
				window.location.href="../login.html";
			},
			error:function(){
				//清理缓存
				CommonUtils.setTempData("userInfo","");
				//跳转页面
				window.location.href="../login.html";
			}
		});
	});
}


